package controller

import (
	"net/http"
	"one-api/model"
	"one-api/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

// GetAllForwardConfigs 获取所有转发配置
func GetAllForwardConfigs(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))

	apiForwardService := &service.APIForwardService{}
	configs, total, err := apiForwardService.GetForwardConfigs(page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": gin.H{
				"message": "Failed to get forward configs",
				"type":    "internal_error",
			},
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data": gin.H{
			"items":     configs,
			"total":     total,
			"page":      page,
			"page_size": pageSize,
		},
	})
}

// CreateForwardConfig 创建转发配置
func CreateForwardConfig(c *gin.Context) {
	var config model.APIForwardConfig
	if err := c.ShouldBindJSON(&config); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": gin.H{
				"message": "Invalid request body",
				"type":    "validation_error",
			},
		})
		return
	}

	apiForwardService := &service.APIForwardService{}
	err := apiForwardService.CreateForwardConfig(&config)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": gin.H{
				"message": "Failed to create forward config",
				"type":    "internal_error",
			},
		})
		return
	}

	c.JSON(http.StatusCreated, gin.H{
		"success": true,
		"data":    config,
		"message": "Forward config created successfully",
	})
}

// UpdateForwardConfig 更新转发配置
func UpdateForwardConfig(c *gin.Context) {
	var config model.APIForwardConfig
	if err := c.ShouldBindJSON(&config); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": gin.H{
				"message": "Invalid request body",
				"type":    "validation_error",
			},
		})
		return
	}

	apiForwardService := &service.APIForwardService{}
	err := apiForwardService.UpdateForwardConfig(&config)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": gin.H{
				"message": "Failed to update forward config",
				"type":    "internal_error",
			},
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    config,
		"message": "Forward config updated successfully",
	})
}

// DeleteForwardConfig 删除转发配置
func DeleteForwardConfig(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": gin.H{
				"message": "Invalid ID",
				"type":    "validation_error",
			},
		})
		return
	}

	apiForwardService := &service.APIForwardService{}
	err = apiForwardService.DeleteForwardConfig(uint(id))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": gin.H{
				"message": "Failed to delete forward config",
				"type":    "internal_error",
			},
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "Forward config deleted successfully",
	})
}

// GetForwardConfig 获取单个转发配置
func GetForwardConfig(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": gin.H{
				"message": "Invalid ID",
				"type":    "validation_error",
			},
		})
		return
	}

	var config model.APIForwardConfig
	err = model.DB.First(&config, id).Error
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"error": gin.H{
				"message": "Forward config not found",
				"type":    "not_found",
			},
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    config,
	})
}
